import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { PaymentService } from './payment.service';
import { UseLog } from 'src/log/useLog.decorator';
import { UserGuard } from 'src/role/role.guard';
import { ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { CurrentUser } from 'src/user/user.decorator';
import { UserSys } from 'src/user/userSys.entity';
import { NotFound } from 'src/util/http';
import { UserService } from 'src/user/user.service';
import { In } from 'typeorm';
import { RoleEnum } from 'src/role/role.enum';

@Controller('api/payment')
@ApiTags("开销记录")
@UseGuards(AuthGuard('jwt'))
export class PaymentController {
    constructor(
        private readonly paymentService: PaymentService,
        private readonly userService: UserService
    ) { }

    @Get("get")
    @UseLog("获取开销记录")
    @UseGuards(UserGuard())
    async get(
        @CurrentUser() user: UserSys

    ) {
        return await this.paymentService.paymenr.find({
            order: {
                id: "DESC"
            },
            relations: ["system", "user"]
        })
    }

    @Post("add")
    @UseLog("新增开销记录")
    @UseGuards(UserGuard())
    async add(
        @CurrentUser() user: UserSys,
        @Body() body
    ) {
        const entity = this.paymentService.paymenr.create({
            user,
            system: user.system,
            ...body
        })
        return await this.paymentService.paymenr.save(entity)
    }

    @Post(":id/setReimbursement")
    @UseGuards(UserGuard([RoleEnum.director]))
    @UseLog("设置是否报销")
    async setReimbursement(
        @Param("id") id: number,
        @Body() { status }
    ) {
        return await this.paymentService.paymenr.update({
            id
        }, {
            reimbursement: (status + "") === "true"
        })
    }

    @Post("del")
    @UseGuards(UserGuard([RoleEnum.director]))
    @UseLog("删除开销记录")
    del(
        @Body() { ids }
    ) {
        return this.paymentService.paymenr.delete(ids)
    }

    @Post(":id/upd")
    @UseLog("修改开销记录")
    @UseGuards(UserGuard())
    async upd(
        @Body() { reimbursement, ...body },
        @Param("id") id: number
    ) {
        return await this.paymentService.paymenr.update({
            id
        }, body)
    }
}
